<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      show-checkbox
      default-expand-all
      check-strictly
      :data="list"
      :props="{label:'name',children:'children'}"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils/index'
import { getRolesDetail, roleAssignPermission } from '@/api/settings'
export default {
  props: {
    releId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 保存权限列表
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 给角色分配权限
    async hSubmit() {
      try {
        // 获取选中的节点
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await roleAssignPermission({ id: this.releId, permIds: permIds })
        console.log(res)
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 加载权限列表
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.list = transDataToTreeData(res.data)
        const info = await getRolesDetail(this.releId)
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        console.log(info, 'info')
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }

}
</script>

<style>

</style>
